define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        // index: function () {
        //     // 初始化表格参数配置
        //     Table.api.init();

        //     var table1 = $("#table1");
        //     console.log(Config);
        //     // 初始化表格
        //     table1.bootstrapTable({
        //         extend: {
        //             index_url: 'command/index' + location.search,
        //             add_url: 'command/add',
        //             edit_url: 'command/edit',
        //             del_url: 'command/del',
        //             multi_url: 'command/multi',
        //             table: 'command',
        //         },
        //         url: 'command/index' + location.search,
        //         pk: 'id',
        //         sortName: 'id',
        //         undefinedText:'',
        //         // search:false,
        //         showToggle:false,
        //         // singleSelect:true,
        //         showExport:false,
        //         toolbar: "#toolbar1",
        //          // 启用固定列
        //         fixedColumns: true,
        //         //固定右侧列数
        //         fixedNumber: 3,

        //         columns: [
        //             [
        //                 {checkbox: true},
        //                 {field: 'id', title: __('序号'),table: table1,width:'40px',formatter:Table.api.formatter.natureorder,operate:false},
        //                 {field: 'name', title: __('Name'),operate:'like',width:'150px'},
        //                 {field: 'investment_total', title: __('总投资(亿元)'), width:'90px',operate:'BETWEEN',
        //                     formatter:function(value,row,index) {
        //                         if(value == -100)
        //                             return '待定';
        //                         else
        //                             return value;
        //                     }
        //                 },
        //                 {field: 'carrier_area', title: __('占地面积(亩)'),width:'90px',operate:'like'},
        //                 {field: 'summary', title: __('项目简介'), width: '260px',operate:false,
        //                     formatter:function(value,row,index){
        //                         if(row.summary&&row.summary.length<=50){
        //                             return row.summary;
        //                         }
        //                         if(row.summary&&row.summary.length>50){
        //                           return row.summary.substring(0,50)+"...";
        //                         }
        //                     }
        //                 },
        //                 {field: 'status_name', title: __('进展情况'),width:'250px',operate:false,
        //                     formatter:function(value,row,index){
        //                         if(row.status_name&&row.status_name.length<50){
        //                             return row.status_name;
        //                         }
        //                         if(row.status_name&&row.status_name.length>50){
        //                           return row.status_name.substring(0,50)+"...";
        //                         }
        //                     }
        //                 },

        //                 {field: 'exist_question', title: __('存在问题'),width:'250px',operate:false,
        //                     formatter:function(value,row,index){
        //                         if(row.exist_question&&row.exist_question.length<=50){
        //                             return row.exist_question;
        //                         }
        //                         if(row.exist_question&&row.exist_question.length>50){
        //                           return row.exist_question.substring(0,50)+"...";
        //                         }
        //                     }
        //                 },

        //                 {field: 'access_time', title: __('计划准入时间'),
        //                     addclass:'datetimerange',
        //                     operate: 'RANGE',width:'150px',
        //                 formatter: Table.api.formatter.datetimedate},

        //                 {field: 'sign_time', title: __('计划签约时间'),
        //                     addclass:'datetimerange',
        //                     operate: 'RANGE',width:'150px',
        //                 formatter: Table.api.formatter.datetimedate},
        //                 {field: 'framework_agreement_time', title: __('签订框架协议时间'),
        //                     addclass:'datetimerange',
        //                     operate: 'RANGE',width:'150px',
        //                 formatter: Table.api.formatter.datetimedate},

        //                 {field: 'invesment_agreement_time', title: __('签订正式投资协议时间'),
        //                     addclass:'datetimerange',
        //                     operate: 'RANGE',width:'150px',
        //                 formatter: Table.api.formatter.datetimedate},


        //                 {field: 'register_time', title: __('注册时间'),
        //                     addclass:'datetimerange',
        //                     operate: 'RANGE',width:'150px',
        //                 formatter: Table.api.formatter.datetimedate},

        //                 {field: 'land_time', title: __('拿地时间'),
        //                     addclass:'datetimerange',
        //                     operate: 'RANGE',width:'150px',
        //                     formatter: Table.api.formatter.datetimedate},

        //                 {field: 'start_construction_time', title: __('开工时间'),
        //                     addclass:'datetimerange',
        //                     operate: 'RANGE',width:'150px',
        //                     formatter: Table.api.formatter.datetimedate},

        //                 {field: 'chargedep_names', title: __('协办单位'),operate:'like',width:'90px'},
        //                 {field: 'settle_street_names', title: __('责任单位'),operate:'like',width:'90px'},

        //                 {field: 'command_type', title: __('专题库类型'),operate: 'like',width:'110px',},

        //                 {field: 'operate', title: __('Operate'),width:'70px', visible: Config.isBuDi?false:true,table: table1, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
        //             ]
        //         ],
        //         // rowStyle:function(row,index){
        //         //     var style = {};
        //         //     // var classes =  ["gray", "red", "yellow", "aqua", "blue", "navy", "teal", "olive", "lime", "fuchsia", "purple", "maroon"];
        //         //     if(Config.isSuperAdmin || Config.uid==35)
        //         //     {
        //         //         if(row.is_add == 1 )
        //         //             style={css:{'color':'#ff0000'}};
        //         //     }

        //         //     if(row.reject_memo_status == 1)
        //         //         style={css:{'color':'blue'}};
        //         //     // console.log(style);
        //         //     return style;
        //         // }
        //     });

        //     // 为表格绑定事件
        //     Table.api.bindevent(table1);
        //     Controller.api.bindeventTable(table1);

        //     var parenttable = table1.closest('.bootstrap-table');
        //     var options = table1.bootstrapTable('getOptions');
        //     var toolbar = $(options.toolbar, parenttable);
        //     $(toolbar).on('click', ".btn-detail", function () {
        //         var that = this;
        //         //循环弹出多个编辑框
        //         $.each(table1.bootstrapTable('getSelections'), function (index, row) {
        //             var url = 'approach/detail';
        //             row = $.extend({}, row ? row : {}, {ids: row[options.pk]});
        //             var url = Table.api.replaceurl(url, row, table1);
        //             var icon = 'fa fa-info';
        //             Backend.api.addtabs(url, row.name, icon);
        //             // Fast.api.open(url, __('详情'), $(that).data() || {});
        //         });
        //     });

        // },

        type: function () {
            $(".btn-add").data("area", ["95%","100%"]);
            $(".btn-edit").data("area", ["95%","100%"]);
            // 初始化表格参数配置
            Table.api.init();
            var table1 = $("#table1");
            // 初始化表格
            table1.bootstrapTable({
                extend: {
                    index_url: 'command/type/typeid/'+Config.typeid + location.search,
                    add_url: 'command/type_add/typeid/'+Config.typeid,
                    edit_url: 'command/edit',
                    del_url: 'command/del',
                    multi_url: 'command/multi/typeid/'+Config.typeid,
                    table: 'command',
                },
                url: 'command/type/typeid/'+Config.typeid + location.search,
                pk: 'id',
                sortName: 'id',
                undefinedText:'',
                // search:false,
                showToggle:false,
                // singleSelect:true,
                showExport:false,
                toolbar: "#toolbar1",
                 // 启用固定列
                fixedColumns: true,
                //固定右侧列数
                fixedNumber: 3,
                columns: Controller.api.columns(table1,Config.typeid),
            });
            // 为表格绑定事件
            Table.api.bindevent(table1);
            Controller.api.bindeventTable(table1);

            $("#btn-import-custom").click(function () {
                var url = 'command/importcommand/typeid/'+Config.typeid;
                Fast.api.open(url, __('Import'), {});
            });
        },

        add: function () {
            Controller.api.bindevent();
        },

        importcommand:function() {
            Controller.api.bindevent();
        },

        type_add: function () {
            $("#c-name").blur(function(){
                var c_name = $("#c-name").val();
                $.ajax({
                    url: 'command/ajaxname',
                    data: {c_name:c_name},
                    type: 'post',
                    dataType: 'json',
                    success: function (ret) {
                        if(ret.code == 1){
                            Layer.alert('项目名称已存在,请更新');
                            $("#c-name").val("");
                        }
                    },
                    error: function (xhr) {

                    }
                }); 
            });

            Controller.api.bindevent();
        },

        detail: function (){
            $(".btn-print").click(function (){
                var html = $(".print").html();
                var bodyHtml = document.body.innerHTML;
                document.body.innerHTML = html;
                window.print();
                document.body.innerHTML = bodyHtml;
            });
        },
        edit: function () {
            Controller.api.bindevent();
        },
        initedit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            bindeventTable: function (table) {
                var parenttable = table.closest('.bootstrap-table');
                //Bootstrap-table配置
                var options = table.bootstrapTable('getOptions');
                // console.log(options);
                //Bootstrap操作区
                var toolbar = $(options.toolbar, parenttable);

                $(toolbar).on('click', ".btn-export", function () {
                    var library_id = $(this).data('library_id');
                    // console.log(library_id);
                    var options = table.bootstrapTable('getOptions');
                    var search = options.queryParams({});
                    var filter = search.filter;
                    var op = search.op;
                    // console.log(filter)
                    // console.log(op)
                    if(library_id == 3)
                        url = 'dispatch/export';
                    // console.log(url + '?filter='+filter+'&op='+op);
                    if(url)
                        location.href= url + '?filter='+filter+'&op='+op;
                });
            },
            columns: function(table,show_check) {
                var pre_columns = [
                    {checkbox: true},
                    {field: '', title: __('序号'),operate:false,table: table,width:'50px',formatter:Table.api.formatter.natureorder},
                    {field: 'name', title: __('Name'),operate:'like',width:'150px'},
                    {field: 'investment_total', title: __('总投资(亿元)'), width:'90px',operate:'BETWEEN',
                        formatter:function(value,row,index) {
                            if(value == -100)
                                return '待定';
                            else
                                return value;
                        }
                    }
                ];
                var operate_columns = [];
                var check_columns = [];
                var common_columns = [];
                if(show_check!=3)
                {
                    common_columns.push({field: 'carrier_area', title: __('占地面积(亩)'),width:'90px',operate:'like'});
                    common_columns.push({field: 'summary', title: __('项目简介'), width: '260px',operate:false,
                        formatter:function(value,row,index){
                            if(row.summary&&row.summary.length<=50){
                                return row.summary;
                            }
                            if(row.summary&&row.summary.length>50){
                              return row.summary.substring(0,50)+"...";
                            }
                        }
                    });
                }
                operate_columns = [
                    // {field: 'summary', title: __('项目简介'), width: '260px',operate:false,
                    //     formatter:function(value,row,index){
                    //         if(row.summary&&row.summary.length<=50){
                    //             return row.summary;
                    //         }
                    //         if(row.summary&&row.summary.length>50){
                    //           return row.summary.substring(0,50)+"...";
                    //         }
                    //     }
                    // },
                    {field: 'status_name', title: __('进展情况'),width:'250px',operate:false,
                        formatter:function(value,row,index){
                            if(row.status_name&&row.status_name.length<50){
                                return row.status_name;
                            }
                            if(row.status_name&&row.status_name.length>50){
                              return row.status_name.substring(0,50)+"...";
                            }
                        }
                    },

                    {field: 'exist_question', title: __('存在问题'),width:'250px',operate:false,
                        formatter:function(value,row,index){
                            if(row.exist_question&&row.exist_question.length<=50){
                                return row.exist_question;
                            }
                            if(row.exist_question&&row.exist_question.length>50){
                              return row.exist_question.substring(0,50)+"...";
                            }
                        }
                    },
                ];

                var time_columns = [];
                if(show_check!=3)
                {

                    time_columns.push({field: 'sign_time', title: __('计划签约时间'),
                        addclass:'datetimerange',
                        operate: 'RANGE',width:'150px',
                        formatter: Table.api.formatter.datetimedate,
                        // 该列标红
                        cellStyle: function (value, row, index) {
                            var style = {};
                            //当前列，单元格显示红色
                            if(row.sign_time>0&&row.is_standard==0)
                            {
                                var timestamp = Date.parse(new Date());//当前时间
                                var end=row.sign_time*1000;//拿地时间
                                var utc=end-timestamp;
                                var day = utc/(24*60*60*1000);// 天
                                console.log(day);
                                console.log(row.sign_time);
                                if(day<10) //10天以内背景标黄
                                    style={css:{'background-color':'yellow'}};
                                if(day<0) //10天以内背景标黄
                                    style={css:{'background-color':'#ff4444','color':"white"}};
                            }
                            return style;
                        }
                    });
                    time_columns.push(
                        {field: 'invesment_agreement_time', title: __('签订正式投资协议时间'),
                            addclass:'datetimerange',
                            operate: 'RANGE',width:'150px',
                            formatter: Table.api.formatter.datetimedate,
                            // 该列标红
                            cellStyle: function (value, row, index) {
                                var style = {};
                                //当前列，单元格显示红色
                                if(row.invesment_agreement_time>0&&row.is_standard==0)
                                {
                                    var timestamp = Date.parse(new Date());//当前时间
                                    var end=row.invesment_agreement_time*1000;//拿地时间
                                    var utc=end-timestamp;
                                    var day = utc/(24*60*60*1000);// 天
                                    console.log(day);
                                    console.log(row.invesment_agreement_time);
                                    if(day<10) //10天以内背景标黄
                                        style={css:{'background-color':'yellow'}};
                                    if(day<0) //10天以内背景标黄
                                        style={css:{'background-color':'#ff4444','color':"white"}};
                                }
                                return style;
                            }
                        },                        
                    );
                }

                var land_columns=[];
                land_columns = [
                    // {field: 'invesment_agreement_time', title: __('签订正式投资协议时间'),
                    //     addclass:'datetimerange',
                    //     operate: 'RANGE',width:'150px',
                    // formatter: Table.api.formatter.datetimedate,
                    //     // 该列标红
                    //     cellStyle: function (value, row, index) {
                    //         var style = {};
                    //         //当前列，单元格显示红色
                    //         if(row.invesment_agreement_time>0&&row.is_standard==0)
                    //         {
                    //             var timestamp = Date.parse(new Date());//当前时间
                    //             var end=row.invesment_agreement_time*1000;//拿地时间
                    //             var utc=end-timestamp;
                    //             var day = utc/(24*60*60*1000);// 天
                    //             console.log(day);
                    //             console.log(row.invesment_agreement_time);
                    //             if(day<10) //10天以内背景标黄
                    //                 style={css:{'background-color':'yellow'}};
                    //             if(day<0) //10天以内背景标黄
                    //                 style={css:{'background-color':'#ff4444','color':"white"}};
                    //         }
                    //         return style;
                    //     }
                    // },

                    {field: 'land_time', title: __('拿地时间'),
                        addclass:'datetimerange',
                        operate: 'RANGE',width:'150px',
                        formatter: Table.api.formatter.datetimedate,
                        // 该列标红
                        cellStyle: function (value, row, index) {
                            var style = {};
                            //当前列，单元格显示红色
                            if(row.land_time>0&&row.is_standard==0)
                            {
                                var timestamp = Date.parse(new Date());//当前时间
                                var end=row.land_time*1000;//拿地时间
                                var utc=end-timestamp;
                                var day = utc/(24*60*60*1000);// 天
                                console.log(day);
                                console.log(row.land_time);
                                if(day<10) //10天以内背景标黄
                                    style={css:{'background-color':'yellow'}};
                                if(day<0) //10天以内背景标黄
                                    style={css:{'background-color':'#ff4444','color':"white"}};
                            }
                            return style;
                        }
                    },

                    {field: 'start_construction_time', title: __('开工时间'),
                        addclass:'datetimerange',
                        operate: 'RANGE',width:'150px',
                        formatter: Table.api.formatter.datetimedate,
                        // 该列标红
                        cellStyle: function (value, row, index) {
                            var style = {};
                            //当前列，单元格显示红色
                            if(row.start_construction_time>0&&row.is_standard==0)
                            {
                                var timestamp = Date.parse(new Date());//当前时间
                                var end=row.start_construction_time*1000;//拿地时间
                                var utc=end-timestamp;
                                var day = utc/(24*60*60*1000);// 天
                                console.log(day);
                                console.log(row.start_construction_time);
                                if(day<10) //10天以内背景标黄
                                    style={css:{'background-color':'yellow'}};
                                if(day<0) //10天以内背景标黄
                                    style={css:{'background-color':'#ff4444','color':"white"}};
                            }
                            return style;
                        }
                    },
                ];
                var oper_columns = [];
                if(show_check!=3)
                {
                    oper_columns.push({field: 'operate_time', title: __('运营时间'),
                        addclass:'datetimerange',
                        operate: 'RANGE',width:'150px',
                        formatter: Table.api.formatter.datetimedate,
                        // 该列标红
                        cellStyle: function (value, row, index) {
                            var style = {};
                            //当前列，单元格显示红色
                            if(row.operate_time>0&&row.is_standard==0)
                            {
                                var timestamp = Date.parse(new Date());//当前时间
                                var end=row.operate_time*1000;//拿地时间
                                var utc=end-timestamp;
                                var day = utc/(24*60*60*1000);// 天
                                console.log(day);
                                console.log(row.operate_time);
                                if(day<10) //10天以内背景标黄
                                    style={css:{'background-color':'yellow'}};
                                if(day<0) //10天以内背景标黄
                                    style={css:{'background-color':'#ff4444','color':"white"}};
                            }
                            return style;
                        }
                    });
                }
                check_columns = [
                    {field: 'chargedep_names', title: __('协办单位'),operate:'like',width:'90px'},
                    {field: 'settle_street_names', title: __('责任单位'),operate:'like',width:'90px'},
                    {field: 'operate', title: __('Operate'),width:'70px', visible: Config.isBuDi?false:true,table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                ];

                var field_columns = pre_columns.concat(common_columns,operate_columns,time_columns,land_columns,oper_columns,check_columns);
                return [field_columns];
            },
        }
    };
    return Controller;
});